package cn.com.headfree.workflow.workflowapprove.controller;

import java.sql.SQLException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gta.edu.sdk.base.controller.BaseController;
import com.gta.edu.sdk.common.constant.Constant;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.common.dto.ResultData;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.workflow.dto.ApproveWorkFlowDto;
import cn.com.headfree.workflow.dto.WorkFlowConditionJSONData;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowApprove;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowNodeApprove;
import cn.com.headfree.workflow.workflowapprove.service.IWorkFlowApproveService;

@CrossOrigin("*")
@RestController
@RequestMapping(value = "/workflow/workflowApprove", produces = "application/json;charset=UTF-8")
public class WorkFlowApproveController extends BaseController {
	private static final Logger log = LogManager.getLogger(WorkFlowApproveController.class);

	@Autowired
	private IWorkFlowApproveService workFlowApproveService;

	/**
	 * @description 流程审批
	 * @author fengya
	 * @date 2015-8-6 上午11:44:09
	 * @param workFlowId
	 * @return
	 * @return String
	 * @throws SQLException
	 */
	@RequestMapping(value = "/saveApproveRecord", method = RequestMethod.POST)
	public ResultData saveApproveRecord(@RequestParam(required = true) String workFlowId, @RequestParam(required = true) String businessId, String workFlowApproveCondition, @RequestParam(required = true) String currentUserId, String approveAdvise, @RequestParam(required = true) String domainId) {
		ResultData result = new ResultData();
		try {
			log.info("workFlowId=" + workFlowId + ",businessId=" + businessId + ",workFlowApproveCondition=" + workFlowApproveCondition + ",currentUserId=" + currentUserId + ",approveAdvise=" + approveAdvise + ",domainId=" + domainId);
			TWorkFlowApprove approve = new TWorkFlowApprove();
			approve.setWorkFlowId(workFlowId);
			approve.setBusinessId(businessId);
			if (!StringUtil.isNullOrEmpty(workFlowApproveCondition)) {
				approve.setApproveCondition(workFlowApproveCondition);
			}
			boolean approveResult = workFlowApproveService.saveApproveRecord(approve, currentUserId, approveAdvise, domainId);
			result.setData(approveResult);
		} catch (Exception e) {
			String errorMsg = "审批工作流出现了异常!";
			log.error(errorMsg, e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	@RequestMapping(value = "/findApprovedRecord/{businessId}", method = RequestMethod.GET)
	public ResultData findApprovedRecord(@PathVariable("businessId") String businessId) {
		ResultData result = new ResultData();
		try {
			List<TWorkFlowNodeApprove> nodeList = this.workFlowApproveService.findApprovedRecord(businessId);
			result.setData(nodeList);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg, e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 获取待办流程
	 * @author fengya
	 * @date 2015-8-6 上午11:44:09
	 * @param workFlowId
	 * @return
	 * @return String
	 * @throws SQLException
	 */
	@RequestMapping(value = "/findWaitApproveWorkFlow/{sysCode}/{currentUserId}/{pageSize}/{pageNo}", method = RequestMethod.GET)
	public ResultData findWaitApproveWorkFlow(@PathVariable("sysCode") String sysCode, @PathVariable("currentUserId") String currentUserId, @PathVariable("pageSize") int pageSize, @PathVariable("pageNo") int pageNo) {
		ResultData result = new ResultData();
		try {
			Page<ApproveWorkFlowDto> page = new Page<ApproveWorkFlowDto>();
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			this.workFlowApproveService.findWaitApproveWorkFlow(page, currentUserId, sysCode);
			result.setData(page);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg, e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 获取待办流程
	 * @author fengya
	 * @date 2015-8-6 上午11:44:09
	 * @param workFlowId
	 * @return
	 * @return String
	 * @throws SQLException
	 */
	@RequestMapping(value = "/findApprovedWorkFlow/{sysCode}/{currentUserId}/{pageSize}/{pageNo}", method = RequestMethod.GET)
	public ResultData findApprovedWorkFlow(@PathVariable("sysCode") String sysCode, @PathVariable("currentUserId") String currentUserId, @PathVariable("pageSize") int pageSize, @PathVariable("pageNo") int pageNo) {
		ResultData result = new ResultData();
		try {
			Page<ApproveWorkFlowDto> page = new Page<ApproveWorkFlowDto>();
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			this.workFlowApproveService.findApprovedWorkFlow(page, currentUserId, sysCode);
			result.setData(page);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg, e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 根据审批ID获取流程审批条件记录数据
	 * @author fengya
	 * @date 2020年7月1日 下午3:57:03
	 * @param approveId
	 * @return ResultData
	 */
	@RequestMapping(value = "/getNextWorkFlowCondition/{approveId}", method = RequestMethod.GET)
	public ResultData getNextWorkFlowCondition(@PathVariable("approveId") String approveId) {
		ResultData result = new ResultData();
		try {
			List<WorkFlowConditionJSONData> list = this.workFlowApproveService.getNextWorkFlowCondition(approveId);
			result.setData(list);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg, e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 审批记录
	 * @author fengya
	 * @date 2015-9-11 下午04:17:26
	 * @param approve
	 * @return
	 * @throws SQLException
	 * @return String
	 */
	@RequestMapping(value = "/approveRecord", method = RequestMethod.POST)
	public ResultData approveRecord(@RequestParam(required = true) String approveId, String approveAdvise, @RequestParam(required = true) String currentUserId, @RequestParam(required = true) String domainId, String approveCondition) {
		ResultData result = new ResultData();
		try {
			TWorkFlowApprove approve = new TWorkFlowApprove();
			approve.setId(approveId);
			approve.setApproveCondition(approveCondition);
			if (this.workFlowApproveService.saveApproveRecord(approve, currentUserId, approveAdvise, domainId)) {
				result.setData(1);
			} else {
				result.setData(0);
			}

		} catch (Exception e) {
			String errorMsg = "审批流程时出现了异常！";
			result.setSuccess(false);
			result.setErrorMsg(Constant.APPLY_UN_SUCCESS);
			log.error(errorMsg, e);
		}
		return result;
	}
}
